/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enteties.article;

import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.article.EancodeDetails;

/**
 *
 * @author Joris
 */
@Stateless
public class EancodeFacade extends AbstractFacade<Eancode> implements EancodeFacadeLocal {
    @PersistenceContext(unitName = "Liebaut-ejbPU")
    private EntityManager em;
    
    @EJB
    private Size1FacadeLocal sizeFacade;
    
    @EJB
    private ModelFacadeLocal modelFacade;
    
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EancodeFacade() {
        super(Eancode.class);
    }
    
    @Override
    public Eancode createEancode (EancodeDetails ed, Article a, Model m, Size1 s){
        Eancode ean = new Eancode();
        ean.setEancode(ed.getEancode());
        ean.setArticleid(a);
        ean.setModelid(m);
        ean.setSizeid(s);
        create(ean);
        return ean;
    }
    
    @Override
    public EancodeDetails convert (Eancode ean){
        if(ean == null)
            return null;
        return new EancodeDetails(ean.getId(), ean.getEancode(),
                sizeFacade.convert(ean.getSizeid()), modelFacade.convert(ean.getModelid()));
    }
    
    @Override
    public Collection<EancodeDetails> convert (Collection<Eancode> eancodes){
        Collection<EancodeDetails> details = new ArrayList<EancodeDetails>();
        for(Eancode eancode : eancodes){
            details.add(convert(eancode));
        }
        return details;
    }
    
}
